import { PackageSourceInfo } from "@src/services/client";
import clientInstance from "@src/services/instance";
import { FcSelect, FcSelectProps } from "solid-fire-controls";
import { Component, createMemo, createResource } from "solid-js";

export interface PackageSourceSelectProps extends FcSelectProps {}

const PackageSourceSelect: Component<PackageSourceSelectProps> = (props) => {
  const load = () =>
    clientInstance.packageSource.listPackageSources().catch((err) => []);

  const [list, { mutate, refetch }] = createResource(load);

  const options = createMemo(() => {
    return [
      new PackageSourceInfo({ name: "Select...", source: "", isEnabled: false }),
      ...(list() ?? []),
    ];
  });

  return (
    <FcSelect
      options={options()}
      optionToLabel={"name"}
      optionToValue={"name"}
      {...props}
    ></FcSelect>
  );
};

export default PackageSourceSelect;
